// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList:[],
    checkAll:true,
    totalPrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCartList()
  },
  onShow: function (options) {
    this.getCartList()
  },
  onClickButton(){
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  // 删除
  onDelete(evt){
    let that = this;
    console.log(evt);
    let index = evt.currentTarget.dataset.id
    wx.request({
      url: 'http://localhost:3000/api/cartdelete',
      method:"POST",
      data:{
        id:index
      },
      success: res => {
        if(res.data.code === 200){
          console.log("删除成功");
          that.getCartList();
          that.calTotalPrice();

        }
      }
    })
  },
  onNumPlus(e){
    wx.request({
      url: 'http://localhost:3000/api/cartadd',
      method:"POST",
      data:{
        uid:this.data.cartList[0].uid,
        goodsid:e.currentTarget.dataset.id,
        num:1
      },
      success: res => {
        if(res.data.code === 200){
          console.log("购物车添加成功");
          let list = this.data.cartList
          list.forEach(item=>{
            if(item.id === e.currentTarget.dataset.id){
              item.num = item.num+1
            }
          })
          this.setData({
            cartList:list
          })
          this.calTotalPrice()
        }
      }
    })
  },
  onNumMinus(e){
    let index = e.currentTarget.dataset.id
    let list1 = this.data.cartList
    let minusNum = -1
    list1.forEach(item=>{
      if(item.id == index){
        if(item.num<2){
          minusNum=0
        }
      }
    })
    console.log(this.data.cartList);
    console.log(e);
    wx.request({
      url: 'http://localhost:3000/api/cartadd',
      method:"POST",
      data:{
        uid:this.data.cartList[0].uid,
        goodsid:e.currentTarget.dataset.id,
        num:minusNum
      },
      success: res => {
        if(res.data.code === 200){
          console.log("购物车减少成功");
          let list = this.data.cartList
          list.forEach(item=>{
            if(item.id === e.currentTarget.dataset.id){
              item.num = item.num-1
            }
          })
          this.setData({
            cartList:list
          })
          this.calTotalPrice()
        }
      }
    })
  },
  // 计算总价
  calTotalPrice(){
    let count = 0;
    let list = this.data.cartList;
    console.log("===========");
    console.log(list);
    console.log("===========");
    list.forEach(item=>{
      if(item.checked){
        count += item.price * item.num
      }
    })
    this.setData({
      totalPrice:count
    })
  },
  allCheckChange(e){
    let check = e.detail
    let list = this.data.cartList
    list = list.map(item => {
      item.checked=check
      return item
    })
    this.setData({
      checkAll:check,
      cartList:list
    })
    this.calTotalPrice()
  },
  singleCheckChange(e){
    let id = e.currentTarget.dataset.id;
    console.log(e.detail);
    let check = e.detail
    let list = this.data.cartList
    list = list.map(item => {
      if(item.id === id){
        let check = e.detail
        item.checked = check
      }
      return item
    })
// 单选控制全选
    let flag = true
    list.forEach(item => {
      if(!item.checked){
        flag=false
      }
    })

    this.setData({
      cartList:list,
      checkAll:flag
    })
    this.calTotalPrice()

  },

  getCartList(){

    let str = wx.getStorageSync('user')
    if(str){
      let user = JSON.parse(str)
      wx.request({
        url: 'http://localhost:3000/api/cartlist?uid='+user.uid,
        success: res => {
          let list = res.data.list
          console.log("++++++++++++++");
          console.log(list);
          console.log("++++++++++++++");
          list = list.map(item =>{
            item.checked=true
            return item
          })
          this.setData({
            cartList:list
          })
          this.calTotalPrice()
        }
      })
      

    }

  },


})